package display.controller;

import java.awt.geom.Point2D;

import display.structure.Wheel;


import prefuse.action.ItemAction;
import prefuse.visual.VisualItem;
import weeny.pre.util.Pre;


public class WheelRotateAnimator extends ItemAction{
	private Point2D startPnt = new Point2D.Double();
	private Point2D   temPnt = new Point2D.Double();
	private double angle = -1;
	protected Wheel wheel = null;
	public Wheel getWheel(){
		return wheel;
	}
	public void setAngle(double angle){
		this.angle = angle;
	}
	public double getAngle(){
		return angle;
	}
	public WheelRotateAnimator(String group, double angle, Wheel wheel) {
		super(group);
		this.angle = angle;
		this.wheel = wheel;
	}
	@Override
	public void process(VisualItem item, double frac) {
		rotate(item, frac);
	}
	public void rotate(VisualItem item, double frac){
		Point2D cent = wheel.center;
		startPnt.setLocation(item.getStartX()-cent.getX(), item.getStartY()-cent.getY());
		double radius = wheel.radius;
		rotate(startPnt, angle, radius, frac, temPnt);
		item.setX(temPnt.getX()+cent.getX());
		item.setY(temPnt.getY()+cent.getY());
		if (frac==1){
			Pre.setPos(item,temPnt.getX()+cent.getX(), temPnt.getY()+cent.getY());
		}
	}
	public static void rotate(Point2D startPnt, double deltaAngle, double radius, double frac, Point2D result){
		double startAngle = Math.atan(startPnt.getY()/startPnt.getX());
		startAngle = startPnt.getX()<0? startAngle+Math.PI : startAngle;
		startAngle +=frac*deltaAngle;
		result.setLocation(radius*Math.cos(startAngle), radius*Math.sin(startAngle));
	}
}
